package com.controleFamiliar.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.controleFamiliar.facade.LoginFacade;
import com.controleFamiliar.form.LoginForm;
import com.controleFamiliar.pojo.Usuario;

public class LoginAction extends DispatchAction{

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginForm formulario = (LoginForm) form;
		//Caso haja algum erro retorna para a tela de login
		String forward = "failure";
		LoginFacade facade = new LoginFacade();
		
		Usuario usuario = facade.validarLogin(formulario);
		
		if(usuario != null){
			forward = "success";
			request.getSession().setAttribute("usuario", usuario);
		}else{
			formulario.limpar();
		}
		
		return mapping.findForward(forward);
	}
}
